"use strict";
/**
 * @ Author: Yoann Meclot. MSay2
 * @ Created on: 2020-08-28 08:22:32
 * @ Modified on: 2021-05-12 13:55:22
 */
/**
 * Copyright (c) 2020-2021 MSay2 (Yoann Meclot) - @msay2/tspoet
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.StringBuilder = void 0;
const Arrays_js_1 = require("./list/Arrays.js");
class StringBuilder {
    constructor(capacity) {
        this.mLength = 0;
        this.mValue = [];
        this.mCapacity = -1;
        if (capacity != undefined)
            this.mCapacity = capacity;
    }
    static fromArray(array) {
        let builder = new StringBuilder();
        for (let item of array)
            builder.append(item);
        return builder;
    }
    length() {
        return this.mLength;
    }
    capacity() {
        return this.mCapacity;
    }
    append(value) {
        if (this.mCapacity == this.mLength)
            return;
        if (value == undefined) {
            this.appendUndefined();
            return;
        }
        if (value == null) {
            this.appendNull();
            return;
        }
        if (this.isOf(value, "String"))
            this.appendString(value);
        if (this.isOf(value, "Boolean"))
            this.appendBoolean(value);
        if (this.isOf(value, "Number"))
            this.appendNumber(value);
        if (value instanceof Object)
            this.appendObject(value);
        return this;
    }
    appendAt(index, value) {
        let clone = Arrays_js_1.Arrays.asList(this.mValue);
        clone.addAt(index, value);
        this.mValue = clone.toArray();
        this.mLength = clone.size();
        return this;
    }
    delete(index) {
        let clone = Arrays_js_1.Arrays.asList(this.mValue);
        clone.remove(index);
        this.mValue = clone.toArray();
        this.mLength = clone.size();
        return this;
    }
    toString() {
        if (this.mLength == 0)
            return "";
        let message = "";
        let index = 0;
        while (index < this.mLength) {
            message += this.mValue[index];
            index++;
        }
        return message;
    }
    clear() {
        this.mLength = 0;
        this.mValue.length = 0;
        this.mCapacity - 1;
    }
    appendString(value) {
        let length = value.length;
        let index = 0;
        while (index < length) {
            let char = value.charAt(index);
            this.mValue[this.mLength++] = char;
            index++;
        }
    }
    appendBoolean(value) {
        this.appendString(value ? "true" : "false");
    }
    appendNumber(value) {
        this.appendString(new String(value).valueOf());
    }
    appendNull() {
        this.appendString("null");
    }
    appendUndefined() {
        this.appendString("undefined");
    }
    appendObject(value) {
        this.appendString(value.constructor.name);
    }
    isOf(object, name) {
        return object.constructor.name == name;
    }
}
exports.StringBuilder = StringBuilder;
//# sourceMappingURL=StringBuilder.js.map